package com.niuke;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 9. 找出两个整数数组中同时出现的整数
 * https://blog.csdn.net/m0_46181452/article/details/131514912
 */
public class NiukeMoni11 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String[] data1 = scanner.nextLine().split(",");
        String[] data2 = scanner.nextLine().split(",");

        // -------------------------------------------------------
        Map<Integer, Integer> dCount1 = new HashMap<>();
        Map<Integer, Integer> dCount2 = new HashMap<>();

        for (int i = 0; i < data1.length; i++) {
            Integer key = Integer.parseInt(data1[i]);
            dCount1.put(key, dCount1.getOrDefault(key, 0) + 1);
        }

        for (int i = 0; i < data2.length; i++) {
            Integer key = Integer.parseInt(data2[i]);
            dCount2.put(key, dCount2.getOrDefault(key, 0) + 1);
        }

        // 注意结果用 LinkedHashMap ，可以对键自动做排序，键代表的是两个数组出现同一个数字的次数
        Map<Integer, List<Integer>> map = new LinkedHashMap<>();
        if (dCount1.size() < dCount2.size()) {
            for (Integer key : dCount1.keySet()) {
                if (dCount2.containsKey(key)) {
                    int minCount = Math.min(dCount1.get(key), dCount2.get(key));
                    List<Integer> list = map.getOrDefault(minCount, new ArrayList<>());
                    list.add(key);
                    map.put(minCount, list);
                }
            }
        } else {
            for (Integer key : dCount2.keySet()) {
                if (dCount1.containsKey(key)) {
                    int minCount = Math.min(dCount1.get(key), dCount2.get(key));
                    List<Integer> list = map.getOrDefault(minCount, new ArrayList<>());
                    list.add(key);
                    map.put(minCount, list);
                }
            }
        }

        if (map.size() == 0) {
            System.out.println("NULL");
        } else {
            for (Integer key : map.keySet()) {
                List<Integer> list = map.get(key);
                if (list != null) {
                    // 输出要对两个数组都有的数字做了排序
                    list.sort(new Comparator<Integer>() {
                        @Override
                        public int compare(Integer integer, Integer t1) {
                            return integer - t1;
                        }
                    });

                    System.out.println(key + ":" + Arrays.toString(list.toArray()));
                }
            }
        }

    }

}
